package com.yiren.manager.ctrl.assets;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.Favorites;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrAsset;
import com.yiren.entity.YrRoomAsset;
import com.yiren.entity.YrRoomAssetDamaged;
import com.yiren.entity.YrStaffInfo;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.assets.YrAssetForm;
import com.yiren.manager.form.assets.YrBaseAssetDamagedForm;
import com.yiren.manager.form.assets.YrBuildingAssetDamagedForm;
import com.yiren.manager.form.assets.YrRoomAssetDamagedForm;
import com.yiren.manager.service.iface.assets.YrAssetService;
import com.yiren.manager.service.iface.assets.YrRoomAssetDamagedService;
import com.yiren.manager.service.iface.assets.YrRoomAssetService;
import com.yiren.manager.service.iface.department.YrStaffInfoService;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("roomAssetDamaged")
@Controller
public class YrRoomAssetDamagedController extends GenericController<YrRoomAssetDamagedForm, YrRoomAssetDamaged>{

	@Autowired
	private YrRoomAssetDamagedService selfService;
	
	@Autowired
	private YrRoomAssetService roomAssetService;
	
	@Autowired
	private YrStaffInfoService staffInfoService;
	
	@Autowired
	private YrAssetService assetService;
		
	//保存实体
	@RequestMapping("/save")
	@ResponseBody
	public HTTPResult<YrRoomAssetDamagedForm> save(String data) throws SwallowException, MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		HTTPResult<YrRoomAssetDamagedForm> result = new HTTPResult<YrRoomAssetDamagedForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrRoomAssetDamagedForm form = tool.mapper("roomAssetDamaged", YrRoomAssetDamagedForm.class);
		if(null==form){
			throwExp("param.error");
		}
		if(null==form.getRoomAssetDamagedId()||form.getRoomAssetDamagedId().isEmpty()){
			form.setRoomAssetDamagedId(UUIDGenerator.getUUID());
			form.setCreateTime(new Date());
		}
		
		YrRoomAsset roomAsset = roomAssetService.findOne(form.getRoomAssetId());
		roomAsset.setQuantity(roomAsset.getQuantity()-form.getDamagedQuantity());
		
		form.setUpdateTime(new Date());
		form.setHandleStatus(0);
		YrRoomAssetDamaged entity = ObjectUtils.objectToObject(form, YrRoomAssetDamaged.class);
		
		Favorites favorites = new Favorites();
		favorites.putFavorite(YrRoomAssetDamaged.class, entity);
		favorites.putFavorite(YrRoomAsset.class, roomAsset);
		
		selfService.addByFavorites(favorites);
		
		return result;
	}
	
	@RequestMapping("findByTime")
	@ResponseBody
	public HTTPResult<YrRoomAssetDamagedForm> findByTime(String startTime,String endTime) throws Exception{
		HTTPResult<YrRoomAssetDamagedForm> result = new HTTPResult<YrRoomAssetDamagedForm>();
		String sqlWhere = "create_time>?1 AND create_time<?2 AND handle_status = 0";
		QueryResult<YrRoomAssetDamaged> qr = selfService.find(YrRoomAssetDamaged.class, sqlWhere, new Object[]{startTime,endTime});
		
		List<YrRoomAssetDamagedForm> formList = new ArrayList<YrRoomAssetDamagedForm>();
		
		for(YrRoomAssetDamaged entity : qr.getResultList()){
			YrRoomAssetDamagedForm form = toForm(entity);
			YrStaffInfo staffInfo = staffInfoService.findOne(entity.getCreateId());
			if(null!=staffInfo){
				form.setHandleName(staffInfo.getName());
			}
			YrRoomAsset roomAsset = roomAssetService.findOne(entity.getRoomAssetId());
			if(null!=roomAsset){
				YrAsset asset = assetService.findOne(roomAsset.getAssetId());
				if(null!=asset){
					form.setAssetForm(ObjectUtils.objectToObject(asset, YrAssetForm.class));
					form.setPrice(asset.getPrice());
				}
			}
			formList.add(form);
		}
		
		result.setData(formList);
		return result;
	}
	
	/**
	 * 修复还原
	 * @param data
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("restore")
	@ResponseBody
	public HTTPResult<YrRoomAssetDamagedForm> restore(String data) throws Exception{
		HTTPResult<YrRoomAssetDamagedForm> result = new HTTPResult<YrRoomAssetDamagedForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrRoomAssetDamagedForm form = tool.mapper("roomAssetDamaged", YrRoomAssetDamagedForm.class);
		form.setHandleStatus(1);
		
		YrRoomAssetDamaged entity = toEntity(form); 
		
		YrRoomAsset roomAsset = roomAssetService.findOne(form.getRoomAssetId());
		roomAsset.setQuantity(roomAsset.getQuantity()+form.getDamagedQuantity());
		
		Favorites favorites = new Favorites();
		favorites.putFavorite(YrRoomAssetDamaged.class, entity);
		favorites.putFavorite(YrRoomAsset.class, roomAsset);
	
		selfService.addByFavorites(favorites);
		
		return result;
	}
	
	@RequestMapping("findAll")
	@ResponseBody
	public HTTPResult<YrBaseAssetDamagedForm> findAll() throws Exception{
		HTTPResult<YrBaseAssetDamagedForm> result = new HTTPResult<YrBaseAssetDamagedForm>();
		List<YrRoomAssetDamaged> list = selfService.findAll();
		
		Map<String, YrBaseAssetDamagedForm> map = new HashMap<String, YrBaseAssetDamagedForm>();
		for(YrRoomAssetDamaged entity : list){
			YrBaseAssetDamagedForm form = null;
			if(map.containsKey(entity.getAssetId())){
				form = map.get(entity.getAssetId());
				Integer qua = form.getDamagedQuantity()+entity.getDamagedQuantity();
				form.setDamagedQuantity(qua);
			}else {
				form = new YrBaseAssetDamagedForm();
				form.setAssetId(entity.getAssetId());
				form.setDamagedQuantity(entity.getDamagedQuantity());
				YrAsset asset = assetService.findOne(form.getAssetId());
				if(null!=asset){
					form.setAssetName(asset.getAssetName());
					form.setPrice(asset.getPrice());
				}
			}
			map.put(entity.getAssetId(), form);
		}
		List<YrBaseAssetDamagedForm> formList = new ArrayList<YrBaseAssetDamagedForm>();
		for(Map.Entry<String, YrBaseAssetDamagedForm> entry : map.entrySet()){
			formList.add(entry.getValue());
		}
		
		result.setData(formList);
		return result;
	}
	
	
	
	@RequestMapping("findBuilding")
	@ResponseBody
	public 	HTTPResult<YrBuildingAssetDamagedForm> findBuilding() throws Exception{
		HTTPResult<YrBuildingAssetDamagedForm> result = new HTTPResult<YrBuildingAssetDamagedForm>();
		List<YrRoomAssetDamaged> list = selfService.findAll();
		
		Map<String, YrBuildingAssetDamagedForm> buildingMap = new HashMap<String, YrBuildingAssetDamagedForm>();
		
		//按楼房分组
		for(YrRoomAssetDamaged roomAsset : list){
			if(buildingMap.containsKey(roomAsset.getBuildingId())){
				List<YrBaseAssetDamagedForm> baseAssetForms = buildingMap.get(roomAsset.getBuildingId()).getBaseAssetDamagedForms();
				boolean flag = false;
				for(YrBaseAssetDamagedForm baseAssetForm : baseAssetForms){
					if(roomAsset.getAssetId().equals(baseAssetForm.getAssetId())){
						baseAssetForm.setDamagedQuantity(baseAssetForm.getDamagedQuantity()+roomAsset.getDamagedQuantity());
						flag = true;
					}
				}
				if(!flag){//如果该资产在列表中没有
					YrBaseAssetDamagedForm baseAssetForm = new YrBaseAssetDamagedForm();
					baseAssetForm.setAssetId(roomAsset.getAssetId());
					baseAssetForm.setAssetName(roomAsset.getAssetName());
					baseAssetForm.setDamagedQuantity(roomAsset.getDamagedQuantity());
					YrAsset asset = assetService.findOne(roomAsset.getAssetId());
					if(null!=asset){
						baseAssetForm.setPrice(asset.getPrice());
					}
					buildingMap.get(roomAsset.getBuildingId()).getBaseAssetDamagedForms().add(baseAssetForm);
				}
			}else {
				YrBuildingAssetDamagedForm buildingAssetForm = new YrBuildingAssetDamagedForm();
				buildingAssetForm.setBuildingId(roomAsset.getBuildingId());
				buildingAssetForm.setBuildingName(roomAsset.getBuildingName());
				
				YrBaseAssetDamagedForm baseAssetForm = new YrBaseAssetDamagedForm();
				baseAssetForm.setAssetId(roomAsset.getAssetId());
				baseAssetForm.setAssetName(roomAsset.getAssetName());
				baseAssetForm.setDamagedQuantity(roomAsset.getDamagedQuantity());
				
				YrAsset asset = assetService.findOne(roomAsset.getAssetId());
				if(null!=asset){
					baseAssetForm.setPrice(asset.getPrice());
				}
				List<YrBaseAssetDamagedForm> baseAssetForms = new ArrayList<YrBaseAssetDamagedForm>();
				baseAssetForms.add(baseAssetForm);
				buildingAssetForm.setBaseAssetDamagedForms(baseAssetForms);
				buildingMap.put(roomAsset.getBuildingId(), buildingAssetForm);
			}
		}
		
		
		List<YrBuildingAssetDamagedForm> formList = new ArrayList<YrBuildingAssetDamagedForm>();
		for(Map.Entry<String, YrBuildingAssetDamagedForm> entry : buildingMap.entrySet()){
			formList.add(entry.getValue());
		}
		result.setData(formList);
		return result;
	}
	
	
	@Override
	public YrRoomAssetDamagedForm toForm(YrRoomAssetDamaged e) throws Exception {
		YrRoomAssetDamagedForm form = ObjectUtils.objectToObject(e, YrRoomAssetDamagedForm.class);
		return form;
	}

	@Override
	public YrRoomAssetDamaged toEntity(YrRoomAssetDamagedForm form) throws Exception {
		YrRoomAssetDamaged entity = ObjectUtils.objectToObject(form, YrRoomAssetDamaged.class);
		return entity;
	}

}
